﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Nizm0.QOT.Options
{
    public partial class frmOptions : Form
    {
        //Library.Objects.ProjectSettings _project;
        private List<Library.Objects.IOption> _optionList;

        public frmOptions()
        {
            InitializeComponent();
            _optionList = Settings.Instance.GetOptions();

            foreach (Library.Objects.IOption option in _optionList)
            {
                string[] path = option.OptionPath.Split('\\');

                TreeNode node;
                TreeNodeCollection nodecoll = treeView_Options.Nodes;
                for (int i = 0; i < path.Length; i++)
                {
                    node = GetNode(nodecoll, path[i]);
                    nodecoll = node.Nodes;
                }
            }
            treeView_Options.ExpandAll();
            //_project = project;
        }

        private TreeNode GetNode(TreeNodeCollection nodes, string name)
        {
            TreeNode n = null;
            bool found = false;
            foreach (TreeNode tmpnode in nodes)
            {
                if (tmpnode.Text == name)
                {
                    found = true;
                    return tmpnode;
                }
            }
            if (!found)
            {
                n = new TreeNode(name);
                nodes.Add(n);
            }
            return n;
        }
        //public frmOptions()
        //{
        //    InitializeComponent();
        //}
        List<UserControl> _settingslist = new List<UserControl>();


        private void treeView_Options_AfterSelect(object sender, TreeViewEventArgs e)
        {
            GetOption(e);
        }

        private void GetOption(TreeViewEventArgs e)
        {
            try
            {
                UserControl mo;
                mo = (UserControl)_optionList.Find(o => o.OptionName == e.Node.Text);
                if (mo == null)
                {
                    mo = (UserControl)_optionList.Find(o => o.OptionName == e.Node.Nodes[0].Text);
                }
                mo.Parent = this;
                mo.Dock = DockStyle.Fill;
                mo.Show();
                mo.BringToFront();
                ((Library.Objects.IOption)mo).Fill();
                bt_SaveAll.BringToFront();
                bt_Ok.BringToFront();
                bt_Cancel.BringToFront();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!!");
            }
        }

        private void bt_SaveAll_Click(object sender, EventArgs e)
        {
            foreach (Control tmpControl in this.Controls)
            {
                foreach (Type t in tmpControl.GetType().GetInterfaces())
                {
                    if (t == typeof(Library.Objects.IOption))
                    {
                        ((Library.Objects.IOption)tmpControl).Save();
                    }
                }
                if (tmpControl.GetType().IsInstanceOfType(typeof(Library.Objects.IOption)))
                {
                    ((Library.Objects.IOption)tmpControl).Save();
                }
                //if (tmpControl.Name == "ucMainOptions")
                //{
                //    ucMainOptions mo = (ucMainOptions)tmpControl;
                //    mo.SaveSettings();
                //}
                //if (tmpControl.Name == "ucSQLOptions")
                //{
                //    ucSQLOptions mo = (ucSQLOptions)tmpControl;
                //    mo.SaveSettings();
                //}
                //if (tmpControl.Name == "usProjectOptions")
                //{
                //    ucSQLOptions mo = (ucSQLOptions)tmpControl;
                //    mo.SaveSettings();
                //}
            }
        }

        private void bt_Ok_Click(object sender, EventArgs e)
        {
            bt_SaveAll_Click(null, null);
            this.Close();
        }

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
